/*  
 *  ------ [802_05b] - receive packets using a 64-bit source address -------- 
 *  
 *  Explanation: This program shows how to receive packets with 
 *  XBee-802.15.4 modules. The source MAC address is read when the
 *  packet is received.
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */

#include <MakeSureXBee802.h>
#include <MakeSureFrame.h>

// variable to store source's network address
uint8_t source_address[8];
uint8_t  PANID[2]={0x33,0x32};

String s;
uint8_t index;
String temp;

//delay between switchning On & Off
long delay_time=500;

//Relay Board Pins connections on Arduino Boards
int pinRelay1=10;
int pinRelay2=11;
int pinRelay3=12;
int pinRelay4=13;
uint8_t buffer;
char* ch;
int a=0;
uint8_t xbeeBaud;

void setup()
{  
 

  // init XBee 
   xbee802.ON();
  //xbee802.setChannel(0x0F);
 /* xbee802.setBaudrate(0x06);
  xbee802.writeValues();

 if(xbee802.error_AT==1)
  {
  Serial.println("Error Setting Baud Rate");
  }
  else if(xbee802.error_AT==0)
  {
  Serial.println("Baud Set Successfully!!");
  }*/

  // Set Arduino Pin Modes that are connected to Relay Board
  pinMode(pinRelay1,OUTPUT);
  pinMode(pinRelay2,OUTPUT);
  pinMode(pinRelay3,OUTPUT);
  pinMode(pinRelay4,OUTPUT);

   // init Serial port

  Serial.begin(9600);
  Serial.println("802_05b example");
}


void loop()
{   
	temp="";
	 
	//xbeeBaud=xbee802.baudrate;
  // check available data in RX buffer
  if( xbee802.available() > 0 )                         //check if data is available
  {
	  Serial.println("Data Available..");
    // parse information
    xbee802.treatData(); 
	//Serial.print("Treatdata");

    // check RX flag after 'treatData'
    if( !xbee802.error_RX ) 
    {
      // check available packets
      while( xbee802.pos>0 )
      {        

        Serial.println("\n\nNew packet received:"); 
        Serial.println("---------------------------"); 

        
        /*** Available info in 'xbee802.packet_finished' structure ***/
        source_address[0]=xbee802.packet_finished[xbee802.pos-1]->macSH[0];
        source_address[1]=xbee802.packet_finished[xbee802.pos-1]->macSH[1];
        source_address[2]=xbee802.packet_finished[xbee802.pos-1]->macSH[2];	
        source_address[3]=xbee802.packet_finished[xbee802.pos-1]->macSH[3];
        source_address[4]=xbee802.packet_finished[xbee802.pos-1]->macSL[0];
        source_address[5]=xbee802.packet_finished[xbee802.pos-1]->macSL[1];
        source_address[6]=xbee802.packet_finished[xbee802.pos-1]->macSL[2];	
        source_address[7]=xbee802.packet_finished[xbee802.pos-1]->macSL[3];


        Serial.println("source_address: ");
        Serial.print(source_address[0],HEX);
        Serial.print(source_address[1],HEX);          //Source_address[] is Array of MAC ID
        Serial.print(source_address[2],HEX);          //Display Mac ID 
        Serial.print(source_address[3],HEX);
        Serial.print(source_address[4],HEX);
        Serial.print(source_address[5],HEX);
        Serial.print(source_address[6],HEX);
        Serial.print(source_address[7],HEX); 
        Serial.println();      

        Serial.println("Data: ");             
		s="";
        for( int i=0 ; i < xbee802.packet_finished[xbee802.pos-1]->data_length ; i++)          
        {           
         s+=String(xbee802.packet_finished[xbee802.pos-1]->data[i]);        //store Data into string s
		}
		Serial.println(s);
		index=s.indexOf("STR") + 4;
		temp=s.substring(index);
		Serial.println(temp);
		Serial.println("\n---------------------------");       
		if(temp=="AA#")
		{
		digitalWrite(pinRelay1,HIGH);
		delay(delay_time);
		digitalWrite(pinRelay1,LOW);
		Serial.println("Relay1 ON");
		}

		else if(temp=="BB#")
		{
		digitalWrite(pinRelay2,HIGH);
		delay(delay_time);
		digitalWrite(pinRelay2,LOW);
		Serial.println("Relay2 ON");
		}
		else if(temp=="CC#")
		{
		digitalWrite(pinRelay3,HIGH);
		delay(delay_time);
		digitalWrite(pinRelay3,LOW);
		Serial.println("Relay3 ON");
		}
		else if(temp=="DD#")
		{
		digitalWrite(pinRelay4,HIGH);
		delay(delay_time);
		digitalWrite(pinRelay4,LOW);
		Serial.println("Relay4 ON");
		}

        // free memory
        free(xbee802.packet_finished[xbee802.pos-1]);
        xbee802.packet_finished[xbee802.pos-1]=NULL; 

        // decrement the received packet counter
        xbee802.pos--; 
      }
    }
  }
} 



